package com.example.administrator.hellocomp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ServiceActivity extends Activity {
private int op;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);
        Button btn_play = (Button)findViewById(R.id.btn_play);
        Button btn_pause = (Button)findViewById(R.id.btn_pause);
        Button btn_stopservice = (Button)findViewById(R.id.btn_stopservice);
        Button btn_replay = (Button)findViewById(R.id.btn_replay);
        Button btn_exit = (Button)findViewById(R.id.btn_exit);

        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId())
                {
                    case R.id.btn_play:
                        op = 1;break;
                    case R.id.btn_pause:
                        op = 2;break;
                    case R.id.btn_replay:
                        op = 3;break;
                    case R.id.btn_stopservice:
                        op = 0;break;
                    case R.id.btn_exit:
                        op = -1;break;
                }
                final Intent intent = new Intent(ServiceActivity.this,MusicService.class);
                if(op!=-1)
                {
                    Bundle bundle = new Bundle();
                    bundle.putInt("op",op);
                    intent.putExtras(bundle);
                    startService(intent);

                }else{
                    //这里是一个退出提升框的操作
                    new AlertDialog.Builder(ServiceActivity.this).setTitle("提示")
                            .setMessage("您确定要退出吗")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    stopService(intent);
                                    finish();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).show();

                }
            }
        };
        btn_play.setOnClickListener(onClickListener);
        btn_exit.setOnClickListener(onClickListener);
        btn_pause.setOnClickListener(onClickListener);
        btn_replay.setOnClickListener(onClickListener);
        btn_stopservice.setOnClickListener(onClickListener);
    }
}
